package com.bw.paper.mvp.model.entity;

import com.bw.paper.mvp.base.model.BaseEntity;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页新闻列表数据
 * {
 *     "values": "[{\"medias\":[{\"media_type\":0,\"media_id\":21,\"media_url\":\"imgs1.jpeg\"}],
 *     \"news_type\":0,\"news_author\":\"编辑1\",
 *     \"news_value\":\"测试新闻1\",
 *     \"news_title\":\"测试1\",
 *     \"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",
 *     \"news_time\":1622029278,
 *     \"author_id\":1,
 *     \"channel_id\":1,
 *     \"news_id\":38},{\"medias\":[{\"media_type\":0,\"media_id\":22,\"media_url\":\"imgs1.jpeg\"},{\"media_type\":0,\"media_id\":23,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻2\",\"news_title\":\"测试2\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":39},{\"medias\":[{\"media_type\":0,\"media_id\":24,\"media_url\":\"imgs1.jpeg\"},{\"media_type\":0,\"media_id\":25,\"media_url\":\"imgs1.jpeg\"},{\"media_type\":0,\"media_id\":26,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻3\",\"news_title\":\"测试3\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":40},{\"medias\":[{\"media_type\":0,\"media_id\":27,\"media_url\":\"imgs1.jpeg\"},{\"media_type\":0,\"media_id\":28,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻4\",\"news_title\":\"测试4\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":41},{\"medias\":[{\"media_type\":0,\"media_id\":29,\"media_url\":\"imgs1.jpeg\"},{\"media_type\":0,\"media_id\":30,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻5\",\"news_title\":\"测试5\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":42},{\"medias\":[{\"media_type\":0,\"media_id\":31,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻6\",\"news_title\":\"测试6\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":43},{\"medias\":[{\"media_type\":0,\"media_id\":32,\"media_url\":\"imgs1.jpeg\"},{\"media_type\":0,\"media_id\":33,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻7\",\"news_title\":\"测试7\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":44},{\"medias\":[{\"media_type\":0,\"media_id\":34,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻8\",\"news_title\":\"测试8\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":45},{\"medias\":[{\"media_type\":0,\"media_id\":35,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻9\",\"news_title\":\"测试9\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":46},{\"medias\":[{\"media_type\":0,\"media_id\":36,\"media_url\":\"imgs1.jpeg\"}],\"news_type\":0,\"news_author\":\"编辑1\",\"news_value\":\"测试新闻10\",\"news_title\":\"测试\",\"news_url\":\"http:\/\/118.195.161.134:8066\/index.html#\/\",\"news_time\":1622029278,\"author_id\":1,\"channel_id\":1,\"news_id\":47}]",
 *     "message": "新闻获取成功",
 *     "status": "200"
 * }
 * */
public class NewsEntity extends BaseEntity {

    public final static int NONECODE = 0;
    public final static int IMG1CODE = 1;
    public final static int IMG2CODE = 2;
    public final static int IMG3CODE = 3;

    public String values;

    public List<Values>getValues(){
        if (values==null)
            return null;
        if (values.isEmpty())
            return null;
        List<Values> list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(values);
            for (int i = 0;i < jsonArray.length();i++){
                String str = jsonArray.getString(i);
                Values entity = new Gson().fromJson(str,Values.class);
                list.add(entity);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static class Values implements MultiItemEntity {
        public int news_type;
        public String news_author;
        public String news_value;
        public String news_title;
        public String news_url;
        public long news_time;
        public long author_id;
        public long channel_id;
        public long news_id;
        public List<MediaValues> medias;

        public List<MediaValues>getMedias(){
//            if (medias.isEmpty())
//                return null;
//            List<MediaValues>medialist = new ArrayList<>();
//            try {
//                JSONArray jsonArray = new JSONArray(medias);
//                for (int i = 0;i < jsonArray.length();i ++){
//                    String str = jsonArray.getString(i);
//                    MediaValues mediaValues = new Gson().fromJson(str,MediaValues.class);
//                    medialist.add(mediaValues);
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
            return medias;
        }

        @Override
        public int getItemType() {
            if (getMedias()==null)
                return NONECODE;
            if (getMedias().size()==0)
                return NONECODE;
            if (getMedias().size() == 1)
                return IMG1CODE;
            if (getMedias().size() == 2)
                return IMG2CODE;
            if (getMedias().size() == 3)
                return IMG3CODE;
            return NONECODE;
        }
    }
    public static class MediaValues{
        public int media_type;
        public long media_id;
        public String media_url;
    }

}
